﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

/*
 * 该窗口功能：
 * 1. 通过DataGridView进行数据预览
 * 2. 通过点击"入库"按钮完成数据入库 
 */

namespace HousingDistribution
{
    public partial class FormExcelPreview : Form
    {
        public FormExcelPreview(string filePath)
        {
            InitializeComponent();
            
            if (filePath == null)
                return;
            
            this.filePath = filePath;
            
            InitErrAddrPersonTable();
        }

        private void FormExcelPreview_Load(object sender, EventArgs e)
        {
            //处理界面布局
            this.tabControlExcelData.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
            this.panelProgressBar.Anchor =  AnchorStyles.Bottom | AnchorStyles.Right;
            this.BtnImportData.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            this.BtnClose.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            this.FormBorderStyle = FormBorderStyle.None;
            this.Dock = DockStyle.Fill;

            //读取数据
            Thread previewThread = new Thread(new ThreadStart(this.Preview));
            previewThread.Start();
        }

        private void ImportData_Click(object sender, EventArgs e)
        {
            if (excelDataSet != null)
            {
                try
                {
                    this.progressBarProc.Value = 0;
                    this.progressBarProc.Maximum = 0;
                    int i = 0;
                    //计算总的数据量(excel数据量的2倍，因为还需插入 排序信息)
                    for (i = 0; i < excelDataSet.Tables.Count; i++)
                    {
                        this.progressBarProc.Maximum +=  excelDataSet.Tables[i].Rows.Count;
                    }
                    
                    CtrlsEnabled(false);

                    Excel2MySQL excel2MySQL = new Excel2MySQL(this.SetProgressBar, ref errAddrPersonTable);

                    for (i = 0; i < excelDataSet.Tables.Count; i++)
                    {
                        excel2MySQL.ProcExcelData(excelDataSet.Tables[i]);
                    }

                    excel2MySQL = null;
                    excelDataSet = null;
            
                    if (this.errAddrPersonTable.Rows.Count > 0)
                    {
                        FormHousingAddrErr errShowForm = new FormHousingAddrErr();
                        errShowForm.SetDataTable(errAddrPersonTable);
                        errShowForm.Show();
                    }

                    this.Close();
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }

                CtrlsEnabled(true);
            }
        }

        private void Close_Click(object sender, EventArgs e)
        {
            excelDataSet = null;
            this.Close();
        }

        private void Preview()
        {
            try
            {
                //读取数据
                ExcelReader excelReader = new ExcelReader();

                excelDataSet = excelReader.Read(filePath);

                if (excelDataSet != null)
                {
                    if (excelDataSet.Tables[PersonTypeEnum.ArmMan.ToString()] != null)
                    {
                        BindData(excelDataSet.Tables[PersonTypeEnum.ArmMan.ToString()], this.dataGridViewArmMan);
                    }

                    if (excelDataSet.Tables[PersonTypeEnum.Staff.ToString()] != null)
                    {
                        BindData(excelDataSet.Tables[PersonTypeEnum.Staff.ToString()], this.dataGridViewStaff);
                    }

                    if (excelDataSet.Tables[PersonTypeEnum.Worker.ToString()] != null)
                    {
                        BindData(excelDataSet.Tables[PersonTypeEnum.Worker.ToString()], this.dataGridViewWorker);
                    }

                    if (excelDataSet.Tables[PersonTypeEnum.NCO.ToString()] != null)
                    {
                        BindData(excelDataSet.Tables[PersonTypeEnum.NCO.ToString()], this.dataGridViewNCO);
                    }

                    if (excelDataSet.Tables[PersonTypeEnum.Unknown.ToString()] != null)
                    {
                        BindData(excelDataSet.Tables[PersonTypeEnum.Unknown.ToString()], this.dataGridViewHousing);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void BindData(DataTable dt, DataGridView dgv)
        {
            if (dgv.InvokeRequired)
            {
                BindData2DGVDelegate d = new BindData2DGVDelegate(BindData);
                this.Invoke(d, new object[] { dt, dgv });
            }
            else
            {
                dgv.DataSource = dt;
            }
        }

        private void SetProgressBar()
        {
            this.progressBarProc.Value += 1;
        }

        private void CtrlsEnabled(bool ctrlFinished)
        {
            this.panelProgressBar.Visible = !ctrlFinished;
            this.BtnImportData.Enabled = ctrlFinished;
            this.BtnClose.Enabled = ctrlFinished;
        }

        void InitErrAddrPersonTable()
        {
            DataColumn name = new DataColumn(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.NAME), typeof(string));
            DataColumn unit = new DataColumn(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.UNIT), typeof(string));
            DataColumn curFullAddr = new DataColumn(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSE_ADDR), typeof(string));

            errAddrPersonTable.Columns.Add(name);
            errAddrPersonTable.Columns.Add(unit);
            errAddrPersonTable.Columns.Add(curFullAddr);
        }
        //错误地址导致无法从房子数据库中找到对应记录的人员存储表
        DataTable errAddrPersonTable = new DataTable();

        private delegate void BindData2DGVDelegate(DataTable dt, DataGridView dgv);
        public delegate void UpdateProgressBarDelegate();

        private DataSet excelDataSet = null;
        private string filePath = null;
    }
}
